# -*- coding:utf-8 -*-

# /*拼多多评价信息*/

import requests,json,time
from common.config import *
from common.Helper import Helper
from threading import Thread
from pprint import pprint


class PddComments( object ):

    def __init__(self):

        pass

    # 页面请求
    def requestIndex(self,task):

        header['cookie'] = task['cookie']

        resp = {'type': 104, 'msg': '评价任务', 'content': {}}

        data = {"type":2,"scoreType":6,"pageSize":10,"pageNo":task['page'],"status":0,"startTime":task['start_time'],"endTime":task['end_time']}

        r = requests.post(url=domain_url['comments_url'],headers=header,data=json.dumps(data) )

        # print( r.json( ))
        # print(r.status_code == 200 and r.json()['result'] == 'null')
        # 判断cookie是否过期
        if r.status_code == 200 and '会话已过期' in json.dumps(r.json(),ensure_ascii=False):

            print('COOKIE已经过期')

            resp['content']['content'] = task
            resp['content']['data'] = {}
            resp['content']['errcode'] = 43001
            resp['content']['errmsg'] = '会话已过期'

            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            return resp


        elif r.status_code == 200 and r.json()['result']:

            items = r.json()['result']['data']

            data = self.parseIndex ( items  )

            resp['content']['content'] = task
            resp['content']['data'] = data
            resp['content']['errcode'] = 0
            resp['content']['errmsg'] = '任务完成'

            pprint(resp)

            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            return resp

        elif r.status_code == 200 and not r.json()['result']:

            print('该时间段无评价信信息')

            resp['content']['content'] = task
            resp['content']['data'] = {}
            resp['content']['errcode'] = 0
            resp['content']['errmsg'] = '任务完成'

            pprint(resp)

            content = Helper.changeBase64(json.dumps(resp['content']))

            resp['content'] = content.decode('ascii')

            return resp


    # 解析信息
    def parseIndex(self,items):

        list = []

        for item in items:

            data = {

                'orderSn':item['orderSn'],
                'createTime':item['createTime'],
                'updateTime':item['updateTime'],
                'descScore':item['descScore'],
                'logisticsScore':item['logisticsScore'],
                'serviceScore':item['serviceScore'],
                'comment':item['comment']
            }

            list.append( data )

        return list



    # api上传
    def apiRequest(self):

        pass



if __name__ == '__main__':

    c = PddComments()

    task = {
        "type": 206,
        "content": {
            "task_page_id": 12,
            "task_id": 1,
            "owner_id": 186716817,
            "start_time": 1561132800,
            "end_time": 1561823999,
            "page": 1,
            "cookie": "api_uid=rBQRGV0ufMgJpwYt0e1qAg==; _nano_fp=XpdjXqXxnqEbnpXyXT_g30sJEPJz34pSjjxCPZK3; PASS_ID=1-tE0KfMKCcjroz5fP24Q1z8JICfav7k/PvdlX7HMr8eSHHkqSndr7qTifW1ovaZhOpVdNMABvQz49PIIagnwYQg_186716817_29159208; JSESSIONID=78C6A09EBE8A2C13DA77D65781573E68"
        },
        "msg": "采集评价任务"
    }

    task = task['content']

    c.requestIndex( task )

